package com.study.xml.junit;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.study.xml.domain.Book;

/**
* @Name: JAXPSAXParseTest
* @Description: JAXP-SAX解析测试类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-2-26 上午11:38:57 
* @Version: V1.0
 */
public class JAXPSAXParseTest {
	
	/**
	* @Name: testSAXNodeInvokeEvent
	* @Description: 测试SAX解析节点触发事件
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-26 上午11:41:01
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void testSAXNodeInvokeEvent() throws Exception {
		//获取SAX解析器工厂
		SAXParserFactory parserFactory = SAXParserFactory.newInstance() ;
		//获取SAX解析器实例对象
		SAXParser saxParser = parserFactory.newSAXParser() ;
		String uri = "src/books.xml" ;
		//解析指定的xml文件
		saxParser.parse(uri, new DefaultHandler() {
			public void startDocument() throws SAXException {
				System.out.println("文档开始了！");
			}
			public void endDocument() throws SAXException {
				System.out.println("文档结束了！");
			}
			public void startElement(String uri, String localName,
					String qName, Attributes attributes) throws SAXException {
				System.out.println("===========================");
				System.out.println("节点开始了！");
				System.out.println("uri : " + uri);
				System.out.println("localName : " + localName);
				System.out.println("qName : " + qName);
				System.out.println("attributes : " + attributes);
			}
			public void endElement(String uri, String localName, String qName)
					throws SAXException {
				System.out.println("===========================");
				System.out.println("节点结束了！");
				System.out.println("uri : " + uri);
				System.out.println("localName : " + localName);
				System.out.println("qName : " + qName);
			}
			public void characters(char[] ch, int start, int length)
					throws SAXException {
				System.out.println("节点文本内容  : " + new String(ch, start, length));
			}
		}) ;
	}
	
	@Test
	public void saveXMLContent2BookBean() throws Exception {
		//获取SAX解析器工厂
		SAXParserFactory parserFactory = SAXParserFactory.newInstance() ;
		//获取SAX解析器实例对象
		SAXParser saxParser = parserFactory.newSAXParser() ;
		//获取xml文档读取器：用于使用回调来读取XML文档
		XMLReader reader = saxParser.getXMLReader() ;
		//设置xml文档内容处理器
		final List<Book> list = new ArrayList<Book>() ;
		reader.setContentHandler(new DefaultHandler(){
			Book book = null ;
			String nodeName = null ;
			public void startElement(String uri, String localName,
					String qName, Attributes attributes) throws SAXException {
				if("书".equals(qName)) {
					book = new Book();
				}
				//标签开始时，记住开始标签名称
				nodeName = qName ;
			}
			public void endElement(String uri, String localName, String qName)
					throws SAXException {
				if("书".equals(qName)) {
					list.add(book) ;
				}
				//标签结束时，清空标签名称
				nodeName = null ;
			}
			public void characters(char[] ch, int start, int length)
					throws SAXException {
				if("书名".equals(nodeName)) {
					book.setName(new String(ch, start, length)) ;
				}
				if("作者".equals(nodeName)) {
					book.setAuthor(new String(ch, start, length)) ;
				}
				if("售价".equals(nodeName)) {
					book.setPrice(Double.valueOf(new String(ch, start, length))) ;
				}
			}
		}) ;
		String uri = "src/books.xml" ;
		//解析xml文件
		reader.parse(uri) ;
		for (Book book : list) {
			System.out.println(book);
		}
	}

}
















